package com.animee.tally;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.animee.tally.adapter.RecordPagerAdapter;
import com.animee.tally.main_frag.IncomeFragment;
import com.animee.tally.main_frag.OutcomeFragment;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView backIv;
    TabLayout tabLayout;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
//        1.查找控件
        backIv = findViewById(R.id.record_iv_back);
        backIv.setOnClickListener(this);
        tabLayout = findViewById(R.id.record_tabs);
        viewPager = findViewById(R.id.record_vp);
//        2.创建ViewPager的页面
        initPager();
    }

    private void initPager() {
        /* 初始化viewpager的页面*/
        List<Fragment>fragmentList = new ArrayList<>();
//        创建两个页面，存放到viewpager的数据源当中
        OutcomeFragment outFrag = new OutcomeFragment();
        IncomeFragment inFrag = new IncomeFragment();
//       添加到集合当中
        fragmentList.add(outFrag);
        fragmentList.add(inFrag);

//        创建适配器
//        ViewPager想要加载数据，需要用到适配器，FragmentPagerAdapter
        RecordPagerAdapter adapter = new RecordPagerAdapter(getSupportFragmentManager(), fragmentList);
//        设置适配器
        viewPager.setAdapter(adapter);
//        将tablayout和viewpager进行关联
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.record_iv_back:
                finish();    //关闭当前acitivty的方法
                break;
        }
    }
}
